package com.ruoyi.yljf.controller.AppController;

import com.aliyun.credentials.http.HttpRequest;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.security.annotation.Anonymous;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.yljf.domain.MedicalRecord;
import com.ruoyi.yljf.domain.dto.CreateDTO.MedicalRecordCreateDTO;
import com.ruoyi.yljf.domain.enums.yljfEnums;
import com.ruoyi.yljf.service.ICollectRecordService;
import com.ruoyi.yljf.service.IMedicalRecordService;
import com.ruoyi.yljf.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 医案Controller
 * 
 * @author metaee
 * @date 2025-02-14
 */

@RestController
@RequestMapping("api/MedicalRecord")
public class ApiMedicalRecordController extends BaseController {
    @Autowired
    private IMedicalRecordService medicalRecordService;
    @Resource
    private ICollectRecordService collectRecordService;
    @Resource
    private CommonUtils commonUtils;

    /**
     * 查询医案列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(MedicalRecord medicalRecord) {
        startPage();
        List<MedicalRecord> list = medicalRecordService.selectMedicalRecordList(medicalRecord);
        return getDataTable(list);
    }
    
    /**
     * 获取医案详细信息
     */
    @Anonymous
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId, HttpServletRequest req) {
        MedicalRecord byId = medicalRecordService.getById(recordId);
        if(byId!=null && commonUtils.userIfLogin(req))
            byId.setCollect(collectRecordService.ifCollect(yljfEnums.NOTICE_TYPE_MEDIAL.getCode(),recordId,getUserId()));
        return success(byId);
    }


    /**
     * 新增医案
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MedicalRecordCreateDTO dto) {
        medicalRecordService.add(dto);
        return success();
    }
    
}
